package com.pzdHome.action;

import java.math.BigDecimal;
import java.util.List;
import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import com.pzdHome.dao.model.Puser;
import com.pzdHome.dao.model.UserRecharge;
import com.pzdHome.service.PuserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;
import com.pzdHome.dao.model.UserPay;
import com.pzdHome.dao.model.UserPayExample;
import com.pzdHome.service.UserPayService;

/**
@author sun
*/
@RequestMapping("userPay")
@Controller
@LoginFilter
public class UserPayController extends CoreController{

    @Autowired
    private UserPayService userPayService;
	@Autowired
	private PuserService puserService;

    @RequestMapping("/list")
	@ResponseBody
    public String list(Integer page, Integer rows){
    	PaginationSupport.byPage(page, rows);
    	UserPayExample example = new UserPayExample();

    	List<UserPay> list = userPayService.selectByExample(example);
      	return page(list);
    }

    @RequestMapping("/save")
	@ResponseBody
    public Ret save(Integer id, Integer payType, String payMoney){
		//查找用户
		Puser puser = puserService.selectByPrimaryKey(id);

		//用户余额是否足够
		BigDecimal banance = new BigDecimal(puser.getMoney());

		//支付金额
		BigDecimal pay = new BigDecimal(payMoney);
		if(banance.compareTo(pay) == -1){
			return new Ret(-1,"当前余额不足");
		}

		UserPay c = new UserPay();
		c.setUserId(id);
		c.setUserAccount(puser.getAccount());
		c.setPayType(payType);
		c.setPayMoney(payMoney);
		userPayService.insert(c);

		//总计金额

		puser.setMoney(banance.subtract(pay).toString());
		puserService.updateByPrimaryKeySelective(puser);
       	return new Ret(0);
    }

    @RequestMapping("/findById")
	@ResponseBody
    public String find(Integer id){
    	UserPay userPay = userPayService.selectByPrimaryKey(id);
       	return ok(userPay);
    }

    @RequestMapping("/del")
	@ResponseBody
    public String del(String id){
    	String[] ids = id.split(",");
    	for (String str : ids){
    		userPayService.deleteByPrimaryKey(Integer.parseInt(str));
    	}
       	return ok();
    }


}